package edu.fpt.tag;

/*
Assignment 1
Class ID        : SE0691
Group           : 1
Due Date        : 20 November 2012
I declare that this assignment is my own work
in accordance with FPT Policy.
*/

import edu.fpt.dal.ApartmentDAL;
import edu.fpt.dal.UsersDAL;
import edu.fpt.hib.Apartment;
import edu.fpt.hib.Users;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 *
 * @author Admin
 */
public class ApartmentListTag extends SimpleTagSupport {

    /**
     * Called by the container to invoke this tag. The implementation of this
     * method is provided by the tag library developer, and handles all tag
     * processing, body iteration, etc.
     */
    @Override
    public void doTag() throws JspException, IOException {
        JspWriter out = getJspContext().getOut();

        try {
            // TODO: insert code to write html before writing the body content.
            // e.g.:
            //
            // out.println("<strong>" + attribute_1 + "</strong>");
            // out.println("    <blockquote>");

            out.println("<table id=\"mytable\" cellspacing=\"0\">");
            out.println("<tr><th scope=\"col\" class=\"nobg\">Apartment</th>"
                    + "<th scope=\"col\">Using by</th>"
                    + "<th scope=\"col\">Ownership</th>"
                    + "<th scope=\"col\">Bill Status</th>"
                    + "<th scope=\"col\">Electric</th>"
                    + "<th scope=\"col\">Water</th>"
                    + "<th scope=\"col\">Bill</th>"
                    + "</tr>");

            ArrayList<Apartment> list = ApartmentDAL.listAllUsing();
            if(list!=null){
            for (Apartment apart : list) {
                Users user = UsersDAL.getUsersHQL(apart.getUsers().getUserId());

                out.println("<tr><th scope=\"row\" class=\"spec\"><a href=\"detailapartment.jsp?apartid=" + apart.getApartId() + "\">" + apart.getApartId() + "</a></td>"
                        + "<td class=\"alt\"><a href=\"detailuser.jsp?userid=" + user.getUserId() + "\">" + user.getUserId() + "</a></td>"
                        + "<td class=\"alt\">");
                if (apart.getIsOwn()) {
                    out.println("Owner");
                } else {
                    out.println("Tenant");
                }
                    out.println("</td>");
                    out.println("<td class=\"alt\">");
                    if (apart.getBillStatus()) {
                        out.println("Paid");
                    } else {
                        out.println("Not Paid");
                    }
                    out.println("</td>");
                    out.println("<td class=\"alt\">" + apart.getElectricNum() + "</td>");
                    out.println("<td class=\"alt\">" + apart.getWaterNum() + "</td>");
                    out.println("<td class=\"alt\">");
                    if (apart.getBillStatus()) {
                        out.println("0");
                    } else {
                        out.println(apart.calculateBill());
                    }
                    out.println("</td>");
                    out.println("</tr>");
                }
                
            
            out.println("</table>");
            }
            // TODO: insert code to write html after writing the body content.
            // e.g.:
            //
            // out.println("    </blockquote>");

        } catch (java.io.IOException ex) {
            throw new JspException("Error in ApartmentListTag tag", ex);
        }
    }
}
